'''
第 0002 题：将 0001 题生成的 200 个激活码（或者优惠券）保存到 sqlite3 数据库中。
'''
import sqlite3
import random


def create():
    res = []
    numer = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
    letter = createLetter()
    choose = letter+numer
    for i in range(0, 200):
        flag = 0
        temp = ''
        for j in range(0,10):
            temp += choose[random.randint(0, len(choose)-1)]
        for k in res:
            if temp == k:
                flag = 1
        if flag == 0:
            res.append(temp)
        else:
            i -= 1
    return res


def createLetter():
    letter = []
    for i in range(65, 91):
        letter.append(chr(i))
    for i in range(97, 123):
        letter.append(chr(i))
    return letter

conn = sqlite3.connect('test.db')
cursor = conn.cursor()
try:
    cursor.execute('create table user (id int primary key, name varchar(20))')
except:
    pass
l = create()
for i in range(0, 200):
    t = l[i]
    print(i, t)
    cursor.execute(r'insert into user values (?, ?)',(i,t))
    cursor.execute(r'select * from user where id=?', (i,))
    conn.commit()
cursor.close()
conn.close()